package com.alks.function.service.impl.stockdept.pcfactorystockquery;

import com.alks.common.data.thread.UserIdThread;
import com.alks.common.service.RedisService;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.biocontrol.deptwork.SoleIoPrintDto;
import com.alks.function.data.dto.stockdept.pcfactorystockwork.StitchDetailDto;
import com.alks.function.data.request.stockdept.pcfactorystockwork.StitchIORequest;
import com.alks.function.mapper.PcSlipDetailSoleMapper;
import com.alks.function.mapper.chengKong.PcLotHeadMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.PcSoleDetailAttachMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcLotDetailMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcSoleDetailMapper;
import com.alks.function.service.stockdept.pcfactorystockquery.ISoleIoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static com.alks.entity.data.enums.RedisKeyEnum.*;
import static com.alks.entity.data.enums.ResultCodeEnum.*;
import static com.alks.function.service.impl.stockdept.pcfactorystockquery.StitchIOServiceImpl.getQtyFieldValues;

@Service
@Slf4j
@RequiredArgsConstructor
public class SoleIoServiceImpl extends ServiceImpl<SD_SQ_PcSoleDetailMapper, PcSoleDetail> implements ISoleIoService {
    private final SD_SQ_PcSoleDetailMapper soleMapper;
    private final PcLotHeadMapper lotHeadMapper;
    private final SD_SQ_PcLotDetailMapper lotDetailMapper;
    private final PcSoleDetailAttachMapper soleAttachMapper;
    private final PcSlipDetailSoleMapper detailSoleMapper;
    private final RedisService redisService;

    private final String soleIn = "I";
    private final String soleOut = "O";

    private final String soleType = "1";
    private final String stitchType = "2";

    @Override
    public ResponseInfo  getSoleIOQuery(String lotNo, String partType, Boolean mergeFlag) {
        String companyId = UserIdThread.get().getCompanyId();
        /*指令判空*/
        if (lotNo==null||lotNo.isEmpty()){
            return ResponseInfo.error("指令不能为空",IN_IS_EMPTY.getCode());
        }
        /*获取型体数据*/
        LambdaQueryWrapper<PcLotHead> wrapperH = new LambdaQueryWrapper<PcLotHead>()
                .select(PcLotHead::getStyle, PcLotHead::getColor, PcLotHead::getDeliveryDate,
                        PcLotHead::getOrderQty, PcLotHead::getSurplusQty)
                .eq(PcLotHead::getLotNo, lotNo)
                .eq(PcLotHead::getCompanyId,companyId);
        /*获取并提取指令型体数据*/
        PcLotHead lotHead = lotHeadMapper.selectOne(wrapperH);
        /*判断数据是否存在*/
        if (lotHead==null){
            return ResponseInfo.error("无效指令", INPUT_ERROR.getCode());
        }
        /*获取数据*/
        List<PcSoleDetail> in = lambdaQuery().eq(PcSoleDetail::getLotNo, lotNo)
                .eq(PcSoleDetail::getSlipType, "I")
                .eq(PcSoleDetail::getPartType,partType)
                .orderByAsc(PcSoleDetail::getSlipDate).list();
        List<PcSoleDetail> out = lambdaQuery().eq(PcSoleDetail::getLotNo, lotNo)
                .eq(PcSoleDetail::getSlipType, "O")
                .eq(PcSoleDetail::getPartType,partType)
                .orderByAsc(PcSoleDetail::getSlipDate).list();
        List<StitchDetailDto> inDtos = soleMapper.listQuery(companyId,partType,"I",lotNo);
        List<StitchDetailDto> outDtos = soleMapper.listQuery(companyId,partType,"O",lotNo);
        for (StitchDetailDto inDto : inDtos) {
            inDto.setTtlQty(getQtyFieldValues(inDto));
        }
        for (StitchDetailDto outDto : outDtos) {
            outDto.setTtlQty(getQtyFieldValues(outDto));
        }

        Float aFloat = ((lotHead.getOrderQty()==null?0:lotHead.getOrderQty()) + (lotHead.getSurplusQty()==null?0:lotHead.getSurplusQty())); //整合为输出语句
        String qty = aFloat + "双（含预补" + lotHead.getSurplusQty() + "双)";

        Map<String, Object> style = new HashMap<>();
        style.put("style", lotHead.getStyle());
        style.put("color", lotHead.getColor());
        style.put("deliveryDate", lotHead.getDeliveryDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        style.put("qty", qty);
        /*获取并处理尺寸和订单数*/
        LambdaQueryWrapper<PcLotDetail> wrapperD = new LambdaQueryWrapper<PcLotDetail>()
                .eq(PcLotDetail::getLotNo, lotNo);
        List<PcLotDetail> details = lotDetailMapper.selectList(wrapperD);
        List<Map<String, String>> size = new ArrayList<>();
        List<Map<String, String>> size2 = new ArrayList<>();
        if (ArrayUtils.isEmpyt(details)){
            log.warn("\n异常接口：getSoleIOQuery\n异常原因：指令 {} 无号码数据\n关键表：【PC_LOT_DETAIL】",lotNo);
        }else {
            size = details.stream()
                    .map(a -> {
                        Map<String, String> map = new HashMap<>();
                        /*避免空指针*/
                        float orderQty = Optional.ofNullable(a.getOrderQty()).orElse(0.0f);
                        float surplusQty = Optional.ofNullable(a.getSurplusQty()).orElse(0.0f);
                        String value;
                        if (mergeFlag!=null && mergeFlag){
                            float sum = orderQty + surplusQty;
                            value = Float.toString(sum);
                        }else {
                            value = orderQty + "+" +surplusQty;
                        }
                        map.put(a.getSizeNo(), value);
                        return map;
                    })
                    .flatMap(map -> map.entrySet().stream())
                    .sorted(Comparator.comparing(entry -> Float.parseFloat(entry.getKey())))
                    .collect(Collectors.groupingBy(
                            Map.Entry::getKey,
                            LinkedHashMap::new,
                            Collectors.mapping(
                                    Map.Entry::getValue,
                                    Collectors.toList() // 改为 toList 保留所有字符串值
                            )
                    ))
                    .entrySet().stream()
                    .map(entry -> {
                        Map<String, String> sortedMap = new HashMap<>();
                        sortedMap.put(entry.getKey(), String.join(",", entry.getValue()));
                        return sortedMap;
                    })
                    .collect(Collectors.toList());
            size2 = details.stream()
                    .map(a -> {
                        Map<String, String> map = new HashMap<>();
                        /*避免空指针*/
                        float orderQty = Optional.ofNullable(a.getOrderQty()).orElse(0.0f);
                        float surplusQty = Optional.ofNullable(a.getSurplusQty()).orElse(0.0f);
                        String value = Float.toString(orderQty + surplusQty);
                        map.put(a.getSizeNo(), value);
                        return map;
                    })
                    .flatMap(map -> map.entrySet().stream())
                    .sorted(Comparator.comparing(entry -> Float.parseFloat(entry.getKey())))
                    .collect(Collectors.groupingBy(
                            Map.Entry::getKey,
                            LinkedHashMap::new,
                            Collectors.mapping(
                                    Map.Entry::getValue,
                                    Collectors.toList() // 改为 toList 保留所有字符串值
                            )
                    ))
                    .entrySet().stream()
                    .map(entry -> {
                        Map<String, String> sortedMap = new HashMap<>();
                        sortedMap.put(entry.getKey(), String.join(",", entry.getValue()));
                        return sortedMap;
                    })
                    .collect(Collectors.toList());
        }
        /*整合数据并输出*/
        Map<String, Object> map = new HashMap<>(6);
        map.put("inDtos", inDtos);
        map.put("outDtos", outDtos);
        map.put("style", style);
        map.put("size", size);
        map.put("size2", size2);
        map.put("sizeTotal", size2);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo soleIOAdd(StitchIORequest request, Integer length, String partType) {
        String companyId = UserIdThread.get().getCompanyId();
        boolean attachFlag = false;
        /*大小写转换*/
        request.setLotNo(request.getLotNo().toUpperCase());
        /*判断是否有填写多余数据*/
        Field[] fields = request.getClass().getDeclaredFields();
        int i=length;
        for (Field field : fields) {
            if (field.getName().equals("qty"+i)){
                i++;
                field.setAccessible(true);
                try {
                    if(field.get(request)!=null){
                        return ResponseInfo.error("超出范围限制");
                    }
                } catch (IllegalAccessException e) {
                    System.err.println("反射没有找到对应元素");
                }
            }
        }
        /*获取待写入数据*/
        PcSoleDetail detail = BeanUtil.copyBean(request, PcSoleDetail.class);
        String slipNoCreat = redisService.slipNoCreat("XD");
        LambdaQueryWrapper<PcSoleDetail> wrapper=new LambdaQueryWrapper<PcSoleDetail>()
                .eq(PcSoleDetail::getSlipNo,slipNoCreat);
        if (soleMapper.selectCount(wrapper)>0){
            slipNoCreat = redisService.updateOrderNumber("XD", slipNoCreat, 5);
        }
        detail.setSysDate(LocalDateTime.now());
        detail.setSysUser(UserIdThread.get().getUserName());
        detail.setCompanyId(companyId);
        detail.setPartType(partType);
        detail.setSlipNo(slipNoCreat);
        /*新增数据*/
        soleMapper.insert(detail);
        return ResponseInfo.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseInfo soleIOUp(StitchIORequest request, String partType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断是否为本人数据*/
        LambdaQueryWrapper<PcSoleDetail> wrapper=new LambdaQueryWrapper<PcSoleDetail>()
                .eq(PcSoleDetail::getSlipNo,request.getSlipNo())
                .eq(PcSoleDetail::getPartType,partType);
        PcSoleDetail detail = soleMapper.selectOne(wrapper);
        if (detail==null){
            throw new ServiceErrorException("订单为空");
        }
        if (!detail.getSysUser().equals(UserIdThread.get().getUserName())){
            return ResponseInfo.error("非本人数据不能修改", USER_IS_ERR.getCode());
        }
        /*判断是流入财务*/
        LambdaQueryWrapper<PcSoleDetailAttach> attachWrapper = new LambdaQueryWrapper<PcSoleDetailAttach>()
                .eq(PcSoleDetailAttach::getCompanyId,companyId)
                .eq(PcSoleDetailAttach::getPartType,partType)
                .eq(PcSoleDetailAttach::getSlipNo,request.getSlipNo());
        financeCheck(companyId, attachWrapper);
        /*更新数据*/
        PcSoleDetail detailU = BeanUtil.copyBean(request, PcSoleDetail.class);
        soleMapper.update(detailU,wrapper);
        attachUp(request.getSlipNo());
        return ResponseInfo.ok();
    }

    /**
     * 检核单号是否流入财务
     * @param companyId 公司ID
     * @param attachWrapper 条件
     */
    private void financeCheck(String companyId, LambdaQueryWrapper<PcSoleDetailAttach> attachWrapper) {
        List<PcSoleDetailAttach> attachList = soleAttachMapper.selectList(attachWrapper);
        if (ArrayUtils.isNoEmpyt(attachList)) {
            Set<String> printNoList = attachList.stream().map(PcSoleDetailAttach::getPrintNo).collect(Collectors.toSet());
            LambdaQueryWrapper<PcSlipDetailSole> priceWrapper = new LambdaQueryWrapper<PcSlipDetailSole>()
                    .in(PcSlipDetailSole::getSlipNo,printNoList)
                    .eq(PcSlipDetailSole::getCompanyId, companyId);
            List<PcSlipDetailSole> priceList = detailSoleMapper.selectList(priceWrapper);
            if (ArrayUtils.isNoEmpyt(priceList)){
                throw new ServiceErrorException("数据已流入财务，变更前请联系财务删除数据");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseInfo soleIODelete(String slipNo, String partType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断是否为本人数据*/
        LambdaQueryWrapper<PcSoleDetail> wrapper=new LambdaQueryWrapper<PcSoleDetail>()
                .eq(PcSoleDetail::getSlipNo,slipNo)
                .eq(PcSoleDetail::getPartType,partType)
                .eq(PcSoleDetail::getCompanyId,companyId);
        PcSoleDetail detail = soleMapper.selectOne(wrapper);
        if (!detail.getSysUser().equals(UserIdThread.get().getUserName())){
            return ResponseInfo.error("非本人数据不能删除", USER_IS_ERR.getCode());
        }
        /*判断是否已被财务获取*/
        LambdaQueryWrapper<PcSoleDetailAttach> attachWrapper=new LambdaQueryWrapper<PcSoleDetailAttach>()
                .eq(PcSoleDetailAttach::getSlipNo,slipNo)
                .eq(PcSoleDetailAttach::getPartType,partType)
                .eq(PcSoleDetailAttach::getCompanyId,companyId);
        financeCheck(companyId, attachWrapper);
//        /*删除汇总数据*/
        soleMapper.delete(wrapper);
//        /*删除明细数据*/
        soleAttachMapper.delete(attachWrapper);
        return ResponseInfo.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String soleSlipNoCreat(String lotNo, String partType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断指令是否存在 && 获取型体*/
        LambdaQueryWrapper<PcLotHead> lotHeadWrapper = new LambdaQueryWrapper<PcLotHead>()
                .eq(PcLotHead::getLotNo,lotNo)
                .eq(PcLotHead::getCompanyId,companyId);
        List<PcLotHead> lotHeads = lotHeadMapper.selectList(lotHeadWrapper);
        if (ArrayUtils.isEmpyt(lotHeads)){
            throw new ServiceErrorException("指令不存在");
        }if (lotHeads.size()>1){
            log.warn("\n异常接口：soleSlipNoCreat\n异常原因：指令单不唯一\n指令号：{}",lotNo);
            throw new ServiceErrorException("指令不唯一");
        }String style = lotHeads.get(0).getStyle();

        /*获取未生成打印单号的收发料数据*/
        List<PcSoleDetail> soleList = soleMapper.unPrintDetail(lotNo,partType,companyId);
        if (ArrayUtils.isEmpyt(soleList)){
            log.warn("\n异常接口：soleSlipNoCreat\n异常原因：指令{}不包含鞋面未汇总的收发明细数据" +
                    "\n相关表：[PC_SOLE_DETAIL] [PC_SOLE_DETAIL_ATTACH]", lotNo);
            return null;
        }
        /*获取号码*/
        LambdaQueryWrapper<PcLotDetail> lotDetailWrapper= new LambdaQueryWrapper<PcLotDetail>()
                .select(PcLotDetail::getSizeNo)
                .eq(PcLotDetail::getLotNo,lotNo)
                .eq(PcLotDetail::getCompanyId,companyId);
        List<PcLotDetail> lotDetails = lotDetailMapper.selectList(lotDetailWrapper);
        List<String> sizeNoList = lotDetails.stream()
                .map(PcLotDetail::getSizeNo)
                .filter(Objects::nonNull)
                .map(Float::valueOf)
                .sorted()
                .map(floatValue -> {
                    if (floatValue == Math.floor(floatValue)) {
                        /*如果是整数，去掉小数部分*/
                        return String.format("%.0f", floatValue);
                    } else {
                        /*如果不是整数，保留小数部分*/
                        return String.valueOf(floatValue);
                    }
                })
                .collect(Collectors.toList());
        /*数据处理*/
        List<PcSoleDetailAttach> attachList = new ArrayList<>(sizeNoList.size() * soleList.size());
        for (PcSoleDetail sole : soleList) {
            for (int i = 0; i < sizeNoList.size(); i++) {
                /*生成待写入数据*/
                PcSoleDetailAttach attach = new PcSoleDetailAttach();
                // 获取对应的qty字段值
                getDetailQty(sole, i, attach);
                attach.setCompanyId(companyId);
                attach.setDeptNo(sole.getDeptNo());
                attach.setDeptName(sole.getDeptName());
                attach.setSlipType(sole.getSlipType());
                attach.setSlipDate(sole.getSlipDate().atStartOfDay());
                attach.setLotNo(lotNo);
                attach.setStyle(style);
                attach.setSlipNo(sole.getSlipNo());
                attach.setPartType(partType);
                attach.setSizeNo(sizeNoList.get(i));
                attachList.add(attach);
            }
        }
        /*处理收料订单*/
        String msg = "";
        List<PcSoleDetailAttach> inAttach = attachList.stream()
                .filter(a -> soleIn.equals(a.getSlipType()))
                .collect(Collectors.toList());
        if (ArrayUtils.isNoEmpyt(inAttach)){
            /*生成打印单号*/
            String inPrintNo = null;
            if (soleType.equals(partType)) {
                inPrintNo = redisService.slipNoCreat(DD_IN.getKey(), DD_IN.getLength());
            } else if (stitchType.equals(partType)) {
                inPrintNo = redisService.slipNoCreat(XD_IN.getKey(), XD_IN.getLength());
            }
            msg = "收料号：'"+inPrintNo+"'";
            /*生成序号 && 写入打印单号*/
            BigDecimal seqNo = BigDecimal.ONE;
            for (PcSoleDetailAttach attach : inAttach) {
                attach.setPrintNo(inPrintNo);
                attach.setSeqNo(seqNo);
                seqNo = seqNo.add(BigDecimal.ONE);
            }
            soleAttachMapper.batchInsert(inAttach);
        }
        /*处理发料订单*/
        List<PcSoleDetailAttach> outAttach = attachList.stream()
                .filter(a -> soleOut.equals(a.getSlipType()))
                .collect(Collectors.toList());
        if (ArrayUtils.isNoEmpyt(outAttach)){
            /*生成打印单号*/
            String outPrintNo = null;
            if (soleType.equals(partType)) {
                outPrintNo = redisService.slipNoCreat(DD_OUT.getKey(), DD_OUT.getLength());
            } else if (stitchType.equals(partType)) {
                outPrintNo = redisService.slipNoCreat(XD_OUT.getKey(), XD_OUT.getLength());
            }
            msg += " 发料号：'"+outPrintNo+"'";
            /*生成序号 && 写入打印单号*/
            BigDecimal seqNo = BigDecimal.ONE;
            for (PcSoleDetailAttach attach : outAttach) {
                attach.setPrintNo(outPrintNo);
                attach.setSeqNo(seqNo);
                seqNo = seqNo.add(BigDecimal.ONE);
            }
            soleAttachMapper.batchInsert(outAttach);
        }

        /*写入数据*/
        return msg;
    }

    @Override
    public SoleIoPrintDto ioPrintf(String printNo, String partType) {
        String companyId = UserIdThread.get().getCompanyId();
        LambdaQueryWrapper<PcSoleDetailAttach> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PcSoleDetailAttach::getPrintNo, printNo);
        queryWrapper.eq(PcSoleDetailAttach::getPartType, partType);
        queryWrapper.eq(PcSoleDetailAttach::getCompanyId, companyId);
        List<PcSoleDetailAttach> dataList = soleAttachMapper.selectList(queryWrapper);
        if (dataList.isEmpty()) {
            throw new ServiceErrorException("打印单不存在");
        }

        // 查询仓管员
        LambdaQueryWrapper<PcSoleDetail> wrapper = new LambdaQueryWrapper<PcSoleDetail>()
                .eq(PcSoleDetail::getSlipNo, dataList.get(0).getSlipNo())
                .eq(PcSoleDetail::getPartType,partType)
                .eq(PcSoleDetail::getCompanyId,companyId);
        PcSoleDetail soleDetail = soleMapper.selectOne(wrapper);

        SoleIoPrintDto soleIoPrintDto = new SoleIoPrintDto();
        soleIoPrintDto.setPrintNo(printNo);
        soleIoPrintDto.setDeptName(soleDetail.getDeptName());
        soleIoPrintDto.setSendNo(soleDetail.getSendNo());
        soleIoPrintDto.setSlipDate(soleDetail.getSlipDate().atStartOfDay());
        soleIoPrintDto.setSysUser(soleDetail.getSysUser());

        // 按指令号分组
        Map<String, List<PcSoleDetailAttach>> dataMap = dataList.stream().collect(
                Collectors.groupingBy(PcSoleDetailAttach::getLotNo));
        List<SoleIoPrintDto.Detail> details = new ArrayList<>();
        dataMap.forEach((key, lotList) -> {
            SoleIoPrintDto.Detail detail = new SoleIoPrintDto.Detail();
            detail.setLotNo(key);
            // 按鞋码排序
            lotList = lotList.stream()
                    .sorted(Comparator.comparing(e -> new BigDecimal(e.getSizeNo())))
                    .collect(Collectors.toList());
            List<SoleIoPrintDto.Item> items = new ArrayList<>();
            for (PcSoleDetailAttach attach : lotList) {
                SoleIoPrintDto.Item item = new SoleIoPrintDto.Item();
                item.setSize(attach.getSizeNo());
                item.setNum(attach.getQty().longValue());
                items.add(item);
            }
            detail.setTotal(items.stream().mapToLong(SoleIoPrintDto.Item::getNum).sum());
            detail.setItems(items);
            details.add(detail);
        });
        soleIoPrintDto.setCountTotal(details.stream().mapToLong(SoleIoPrintDto.Detail::getTotal).sum());
        soleIoPrintDto.setDetails(details);

        /*更新打印标志位*/
        soleAttachMapper.printUp(printNo,partType,companyId);
        return soleIoPrintDto;
    }


    private void attachUp(String slipNo){
        String companyId = UserIdThread.get().getCompanyId();
        /*判断slipNo是否存在*/
        LambdaQueryWrapper<PcSoleDetailAttach> wrapper = new LambdaQueryWrapper<PcSoleDetailAttach>()
                .eq(PcSoleDetailAttach::getCompanyId,companyId)
                .eq(PcSoleDetailAttach::getSlipNo,slipNo);
        Integer count = soleAttachMapper.selectCount(wrapper);
        if (count<=0){
            return;
        }
        /*获取汇总后的数据和对照表*/
        LambdaQueryWrapper<PcSoleDetail> soleWrapper = new LambdaQueryWrapper<PcSoleDetail>()
                .eq(PcSoleDetail::getCompanyId,companyId)
                .eq(PcSoleDetail::getSlipNo,slipNo);
        List<PcSoleDetail> pcSoleDetailList = soleMapper.selectList(soleWrapper);
        PcSoleDetail sole = pcSoleDetailList.get(0);
        String lotNo =  sole.getLotNo();
        String deptNo = sole.getDeptNo();
        String deptName = sole.getDeptName();
        /*获取号码匹配数据*/
        LambdaQueryWrapper<PcLotDetail> detailWrapper = new LambdaQueryWrapper<PcLotDetail>()
                .eq(PcLotDetail::getLotNo,lotNo)
                .eq(PcLotDetail::getCompanyId,companyId);
        List<PcLotDetail> detailList = lotDetailMapper.selectList(detailWrapper);
        /*排序*/
        detailList.sort(Comparator.comparing(PcLotDetail::getSeqNo));
        List<PcSoleDetailAttach> attachList = new ArrayList<>();
        for (int i = 0; i < detailList.size(); i++) {
            PcSoleDetailAttach attach = new PcSoleDetailAttach();
            getDetailQty(sole, i, attach);
            String sizeNo = detailList.get(i).getSizeNo();
            attach.setSizeNo(sizeNo);
            attach.setDeptNo(deptNo);
            attach.setDeptName(deptName);
            attach.setSlipNo(slipNo);
            attach.setPartType(sole.getPartType());
            attachList.add(attach);
        }
        /*更新明细数据*/
        soleAttachMapper.attachUp(attachList,companyId);
    }

    /**
     * 获取指定数量
     */
    private void getDetailQty(PcSoleDetail sole, int i, PcSoleDetailAttach attach) {
        String qtyFieldName = "qty" + (i + 1);
        try {
            String getterMethodName = "get" + qtyFieldName.substring(0, 1).toUpperCase() + qtyFieldName.substring(1);
            Method getterMethod = PcSoleDetail.class.getMethod(getterMethodName);
            BigDecimal qtyValue = (Float) getterMethod.invoke(sole)==null?BigDecimal.ZERO:BigDecimal.valueOf((Float) getterMethod.invoke(sole));
            attach.setQty(qtyValue);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            log.error("\n错误接口：soleSlipNoCreat\n错误原因：获取qty字段值失败: {}\n", e.getMessage(), e);
            throw new ServiceErrorException("生成失败");
        }
    }

}
